/**
 * 
 */
package org.yanglk.demo.redis.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 * @author yanglk <br>
 *         Create Time：2018年4月23日下午3:18:13<br>
 *         <a href="mailto:yanglk2010@sina.com">yanglk2010@sina.com</a>
 * @since 1.0
 * @version 1.0
 */

@Configuration
@ComponentScan({ "org.yanglk.demo.redis.service" })
@EnableCaching
public class RedisConfig {
	@Value("${spring.redis.host}")
	private String host;
	@Value("${spring.redis.password}")
	private String passwd;

	@Bean(name = "redisFactory")
	public RedisConnectionFactory connectionFactory() {
		// RedisStandaloneConfiguration conf = new
		// RedisStandaloneConfiguration("localhost", 6379);
		// conf.setPassword(RedisPassword.of("123456"));
		JedisConnectionFactory factory = new JedisConnectionFactory();
		factory.getStandaloneConfiguration().setPassword(RedisPassword.of(passwd));
		// factory.setHostName("yanglk_aliyun");
		// factory.setPassword("Yanglk123456");
		// factory.setHostName("10.124.51.29");
		// factory.setHostName(host);
		// factory.setPassword(passwd);
		return factory;
	}

	@Bean
	public Jackson2JsonRedisSerializer<?> jackson2JsonRedisSerializer() {
		Jackson2JsonRedisSerializer<?> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(
				Object.class);
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		jackson2JsonRedisSerializer.setObjectMapper(om);
		return jackson2JsonRedisSerializer;
	}

	@Bean(name = "redisTemplate")
	public <VALUE> RedisTemplate<String, ?> redisTemplate(@Qualifier("redisFactory") RedisConnectionFactory cf) {
		RedisTemplate<String, ?> redisTemplate = new RedisTemplate<String, VALUE>();
		redisTemplate.setConnectionFactory(cf);
		redisTemplate.setDefaultSerializer(jackson2JsonRedisSerializer());
		return redisTemplate;
	}

	@Bean
	public CacheManager cacheManager() {
		RedisCacheManager cacheManager = RedisCacheManager.create(connectionFactory());
		return cacheManager;
	}

}
